// const { get } = require("mobx-miniprogram")

const app = getApp()
//同步请求api
const sync = app.globalData.sync


Component({
    /**
     * 组件的属性列表
     */
    properties: {
       
    },

    /**
     * 组件的初始数据
     */
    data: {
        aquariumId: Number,
        fishList: [],
        fishIndex: 0,
        id: '',
        pid: Number,
        // 鱼的年龄
        age: Number,
        fishName: '',
        type: '',
        state: '',
        illType: '',
        fishImg: '',
        gmtCreate: '',

        array: [],
        // curFishName:'',

    },

    /**
     * 组件的方法列表
     */
    methods: {
        addAquarium() {
            wx.redirectTo({
                url: '../../pages/index/index?selectIndex=4',
            })
        },

        delete() {
            wx.request({
                url: `http://120.77.156.205:8808/wx/aquarium/deleteFish/${this.data.aquariumId}/${this.data.id}`,
                method: 'GET',
                success: (res) => {
                    console.log(res)
                    if (res.data.code == 0) {
                        wx.showToast({
                            title: '删除成功！',
                            icon: 'success',
                            duration: 1000
                        })
                        wx.reLaunch({
                            url: '../../pages/index/index?selectIndex=2',
                        })
                    } else {
                        wx.showToast({
                            title: '删除失败！',
                            icon: 'error',
                            duration: 1000
                        })
                    }
                },
                fail: (err) => {
                    console.log(err)
                }
            })
        },



        edit1() {
            console.log(this.data.illType)
            wx.navigateTo({
                url: `../../pkgA/editFish/editFish?aquariumId=${this.data.aquariumId}&id=${this.data.id}&fishName=${this.data.fishName}&age=${this, this.data.age}&type=${this.data.type}&state=${this.data.state}&illType=${this.data.illType}`,
            })
        },

        addFish() {
            //如果鱼缸满，则不允许跳转到添加鱼的页面
            wx.request({
                url: `http://120.77.156.205:8808/wx/aquarium/addFish/${this.data.aquariumId}`,
                data: {
                },
                method: 'POST',
                success: (res) => {
                    console.log(res)
                    if (res.data.code == 10) {
                        console.log("水族箱鱼满")
                        wx.showToast({
                            title: '水族箱达到最大容量!',
                            icon: 'none',
                            duration: 2000
                        })
                        return
                    } else {
                        //鱼缸没满，则跳转
                        wx.navigateTo({
                            url: '../../pkgA/addFish/addFish',
                        })
                    }
                }
            })



        },

        onChange(event) {
            const { picker, value, index } = event.detail;
            Toast(`当前值：${value}, 当前索引：${index}`);
        },

        selectFish(e) {
            let curIndex = e.detail.value
            this.setData({
                fishIndex: curIndex,
                id: this.data.fishList[curIndex].id,
                pid: this.data.fishList[curIndex].pid,
                age: this.data.fishList[curIndex].inAge,
                fishName: this.data.fishList[curIndex].name,
                fishImg: this.data.fishList[curIndex].fishImg,
                state: this.data.fishList[curIndex].state,
                type: this.data.fishList[curIndex].type,
                illType: this.data.fishList[curIndex].illType,
                gmtCreate: this.data.fishList[curIndex].gmtCreate,

            })
            console.log(this.data.id)
        },

        save() {
            if (this.data.id.length === 0) {
                console.log("'没有鱼！")
                wx.showToast({
                    title: '没有鱼！',
                    icon: "error",
                    duration: 2000
                })
                return
            }

            // console.log(this.data.fishImg)

            wx.downloadFile({
                url: this.data.fishImg,
                success: function (res) {
                    wx.saveImageToPhotosAlbum({
                        filePath: res.tempFilePath,
                        success(res) {
                            wx.showToast({
                                title: '保存成功',
                                icon: 'success',
                                duration: 2000
                            })
                        },
                        fail(res) {
                            wx.showToast({
                                title: '保存图片失败',
                                icon: 'error',
                                duration: 2000
                            })
                        }

                    })


                }
            })

        },

        edit() {

            if (this.data.id.length === 0) {
                console.log("'没有鱼！")
                wx.showToast({
                    title: '没有鱼！',
                    icon: "error",
                    duration: 2000
                })
                return
            }
            let _this = this;
            wx.showActionSheet({
                itemList: ['从相册中选择', '拍一张'],
                itemColor: "#f7982a",
                success(res) {
                    if (!res.cancel) {
                        if (res.tapIndex == 0) {
                            // 从相册中选择
                            _this.chooseWxImage('album')
                        } else if (res.tapIndex == 1) {
                            // 使用相机
                            _this.chooseWxImage('camera')
                        }
                    }
                }
            })
        },
        // 选择图片
        chooseWxImage(type) {
            let _this = this;
            let imgs = this.data.imageList;
            wx.chooseImage({
                count: 1,
                sizeType: ['original', 'compressed'],
                sourceType: [type],
                success(res) {
                    // if (imgs.length > 2) {
                    //     return wx.showToast({
                    //         title: '最多可上传三张图片',
                    //         icon: 'none'
                    //     })
                    // }
                    _this.upload(res.tempFilePaths[0]);
                }
            })
        },
        //上传图片到服务器
        upload: function (path) {
            console.log("上次服务器请求发送！")
            let _this = this;
            // console.log(path)
            wx.showToast({
                icon: "loading",
                title: "正在上传"
            }),
                //将本地资源上传到服务器
                wx.uploadFile({
                    url: `http://120.77.156.205:8808/wx/aquarium/uploadFishPic/${this.data.id}`,    // 开发者服务器地址
                    filePath: path,   // 要上传文件资源的路径 (本地路径)
                    name: 'pic', //服务端接收的key，就是接口参数MultipartFile的参数名
                    success: function (res) {
                        console.log(res)
                        let resData = JSON.parse(res.data)
                        // 给数组中的某个下标改值
                        let index = _this.data.fishIndex
                        let curFish = 'fishList[' + index + '].fishImg'
                        console.log(_this.data.fishList[_this.data.fishIndex].fishImg)
                        console.log(curFish)
                        if (resData.code == 0) {
                            wx.showToast({
                                title: '修改成功',
                                icon: 'success',
                                duration: 2000
                            })
                            _this.setData({
                                fishImg: resData.data.picUrl,
                                [curFish]: resData.data.picUrl,
                            })
                            console.log(_this.data.fishList[_this.data.fishIndex].fishImg)
                        }
                    },
                    fail: function (e) {
                        wx.showToast({
                            title: '修改失败',
                            icon: 'error',
                            duration: 2000
                        })
                    },
                    complete: function () {
                        wx.hideToast(); //隐藏Toast
                    }
                })
        },
    },

    lifetimes: {

        attached() {
            this.setData({
                aquariumId: wx.getStorageSync('userAquariumList')[wx.getStorageSync('curIndex')]
            })

            sync.getData(`http://120.77.156.205:8808/wx/aquarium/getAllFish/${this.data.aquariumId}`)
                .then((res) => {

                    var fishList = res.data.fishList
                    if (res.data.fishList.length != 0) {
                        this.setData({
                            fishList: fishList,
                            id: fishList[0].id,
                            pid: fishList[0].pid,
                            age: fishList[0].inAge,
                            fishName: fishList[0].name,
                            type: fishList[0].type,
                            illType: fishList[0].illType,
                            state: fishList[0].state,
                            fishImg: fishList[0].fishImg,
                            gmtCreate: fishList[0].gmtCreate,
                        })

                        var newArray = []
                        fishList.forEach(fish => {
                            newArray.push(fish.name)
                        });

                        this.setData({
                            array: newArray,

                        })

                        console.log(this.data.fishImg)
                    } else {
                        console.log("没有添加鱼的记录")

                    }

                })

        },
        show() {
            this.attached()
        }

    }
})
